//
//  PointDetailController.m
//  HB
//
//  Created by 陶秋安 on 16/5/16.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "PointDetailController.h"

@implementation PointDetailController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    arrayData=[[NSMutableArray alloc] init];
    
    _tvPageDetail.dataSource=self;
    
    _tvPageDetail.delegate= self;

    [self myPoint];
}

-(void)setNavigationLeft:(NSString *)imageName sel:(SEL)sel
{
    
    
}

- (void)setNavigationRight:(NSString *)imageName sel:(SEL)sel
{
   
    
}

- (void)setNavigationTitleImage:(NSString *)imageName
{
    self.navigationItem.title = @"积分明细";
}



-(void)myPoint
{
    operation= [[PointOperation alloc] initWithDelegate:self];
    
    currPageIndex=1;
    
    pageSize=18;
    
    NSString *djID= [Common getUserDefaults:@"djID"];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"DJPointDetail"];
    
    NSString *finallURL= [NSString stringWithFormat:@"%@?djID=%@&pageindex=%ld&pagesize=%ld",URL,djID,(long)currPageIndex,(long)pageSize];
    
    NSDictionary * dictInfo = @{@"url":finallURL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

-(void)opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    if(flag==1)
    {
        operation= nil;
        
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        if(flag ==1)
        {
 
            
            if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
            {
                NSInteger totlePageCount= [[dataParse objectForKey:@"totlePageCount"] intValue];
                
                currPageIndex= [[dataParse objectForKey:@"currPageIndex"] intValue];
                
                if(totlePageCount>1 && currPageIndex < totlePageCount)
                {
                    _hasNextPage=YES;
                }
                else
                {
                    _hasNextPage= NO;
                }
                
                NSArray *_array = [dataParse objectForKey:@"data"];
                
                for (NSInteger i=0; i<_array.count; i++)
                {
                    
                    NSDictionary *dict= [_array objectAtIndex:i];
                    
                    NSString *Way= [dict objectForKey:@"Way"];
                    NSMutableString *itemid= [dict objectForKey:@"id"];
                    NSString *CreateTime= [dict objectForKey:@"CreateTime"];
                    NSMutableString *Money= [dict objectForKey:@"Money"];
                    
                     NSDictionary *finalldict = [NSDictionary dictionaryWithObjectsAndKeys:
                            
                            Way,@"Way",
                            [NSString stringWithString:itemid],@"id",
                            CreateTime,@"CreateTime",
                                                 [NSString stringWithString:Money],@"Money",
                                                 nil];

                    [arrayData addObject:finalldict];
                }

            }
        }
        
        [self.tvPageDetail performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    // 次方法是在拖动表格时 单元格显示在表格的bounds 范围内时触发
    if (indexPath.row < arrayData.count)
    {
        NSString *idefine = @"PointDetail";
        
        PointDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idefine];
        
        if (cell==nil)
        {
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:idefine owner:tableView options:nil];
            
            cell = [Objects objectAtIndex:0];
        }
        
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
        NSInteger i=  indexPath.row;
        
        NSDictionary *pointDetailInfo= [arrayData objectAtIndex:i];
        
        NSString *amount=[pointDetailInfo objectForKey:@"Money"];
        
        cell.lblMoney.text = amount;//
        
        //NSString *way= [pointDetailInfo objectForKey:@"Way"];
        
        NSMutableString *way=[pointDetailInfo objectForKey:@"Way"];
        
        cell.lblTitle.text = [NSString stringWithFormat:@"积分%@",way] ;
        
        cell.lblCreateTime.text = [pointDetailInfo objectForKey:@"CreateTime"];
        
        cell.ID= [[pointDetailInfo objectForKey:@"id"] integerValue];
        
        return cell;

    }
    else
    {
        NSString  *cellIdentifier = @"NewsMoreCell";
        
        MoreCell *cell = (MoreCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell == nil) {
            
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
            
            cell = [Objects objectAtIndex:0];
            
        }
        
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
}


#pragma mark 设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


#pragma mark 设置表格的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}

#pragma mark 设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _hasNextPage?arrayData.count+1:arrayData.count;
}

@end

